from datetime import datetime
from django.db import models
from DjangoUeditor.models import UEditorField

from apps.organizations.models import Org, Teacher


class Course(models.Model):
    """
    课程
    """
    # 机构与课程一对多的关系
    org = models.ForeignKey(Org,verbose_name=u"授课机构")
    # 讲师与课程一对多的关系
    teacher = models.ForeignKey(Teacher,verbose_name=u"授课讲师")
    category = models.CharField(verbose_name=u"课程类别",max_length=20,default="后台开发")
    isBanner = models.BooleanField(verbose_name=u"是否轮播",default=False)
    name = models.CharField(verbose_name=u"名称",max_length=100)
    desc = models.TextField(verbose_name=u"描述",max_length=200)
    detail = UEditorField(verbose_name=u"详情", width=600, height=300, imagePath="course/ueditor/%(basename)s_%(datetime)s.%(extname)s", filePath="course/ueditor/%(basename)s_%(datetime)s.%(extname)s")
    degree = models.CharField(verbose_name=u"等级",max_length=2,choices=(("cj","初级"),("zj","中级"),("gj","高级")))
    learnTimes = models.IntegerField(verbose_name=u"学习时长(分钟)",default=0)
    students = models.IntegerField(verbose_name=u"学习人数",default=0)
    favNum = models.IntegerField(verbose_name=u"收藏人数",default=0)
    clickNum = models.IntegerField(verbose_name=u"点击数",default=0)
    images = models.ImageField(verbose_name=u"封面图片",upload_to="course/images/%Y/%m/%d",null=True,blank=True)
    addTime = models.DateTimeField(verbose_name=u"添加时间",default=datetime.now)
    tag = models.CharField(verbose_name=u"课程标签",default="",max_length=10)
    youKnow = models.CharField(verbose_name=u"课程须知",max_length=100,default="")
    teacherTell = models.CharField(verbose_name=u"老师寄语",max_length=100,default="")

    class Meta:
        verbose_name = u"课程"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    # 课程章节信息
    def getCourseLessons(self):
        return self.lesson_set.all()

    # 课程学习人数信息
    def getCourseUsersForFive(self):
        return self.usercourse_set.all()[:5]

    def getCourseUsers(self):
        return self.usercourse_set.all()

    # 课程资源文件信息
    def getCourseResources(self):
        return self.courseresource_set.all()

    # 课程评论信息
    def getCourseComments(self):
        return self.coursecomments_set.all()

    def getCourseLessonNum(self):
        return self.lesson_set.all().count()

    def getCourseResourceNum(self):
        return self.courseresource_set.all().count()

    # 将该方法的结果以字段的形式显示在列表中
    getCourseLessonNum.short_description = "章节"
    getCourseResourceNum.short_description = "附件"


class BannerCourse(Course):
    """
    轮播课程
    """
    class Meta:
        verbose_name = "轮播课程"
        verbose_name_plural = verbose_name
        proxy = True


class CourseResource(models.Model):
    """
    课程-资源
    """
    # 课程和课程资源属于一对多的关系
    course = models.ForeignKey(Course,verbose_name=u"课程")
    name = models.CharField(verbose_name=u"文件名称",max_length=100)
    addTime = models.DateTimeField(verbose_name=u"添加时间", default=datetime.now)
    download = models.FileField(verbose_name=u"上传地址", max_length=100,upload_to="course/resource/%Y/%m/%d")

    class Meta:
        verbose_name = u"课程资源"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Lesson(models.Model):
    """
    课程-章节
    """
    # 课程与张节属于一对多的关系
    course = models.ForeignKey(Course,verbose_name=u"课程")
    name = models.CharField(verbose_name=u"章节名",max_length=100)
    addTime = models.DateTimeField(verbose_name=u"添加时间", default=datetime.now)

    class Meta:
        verbose_name = u"课程章节"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    # 获取章节视频
    def getLessonVideos(self):
        return self.video_set.all()


class Video(models.Model):
    """
    章节-视频
    """
    # 章节与视频属于一对多的关系
    lesson = models.ForeignKey(Lesson,verbose_name=u"章节")
    name = models.CharField(verbose_name=u"视频名称",max_length=100)
    addTime = models.DateTimeField(verbose_name=u"添加时间",default=datetime.now)
    urlAddress = models.CharField(verbose_name=u"访问地址",max_length=200,default="")

    class Meta:
        verbose_name = u"章节视频"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
